#include "listdelegate.h"

#include <QtGui/QTextEdit>

using namespace gui;

ListDelegate::ListDelegate(QWidget* parent) :
    QStyledItemDelegate(parent),
    m_role(Qt::EditRole)
{

}

ListDelegate::~ListDelegate()
{

}

void ListDelegate::setRole(Qt::ItemDataRole role)
{
    m_role = role;
}

QWidget* ListDelegate::createEditor(QWidget* parent,
    const QStyleOptionViewItem& option,
    const QModelIndex& index) const
{
    QTextEdit* editor = new QTextEdit(parent);
    editor->installEventFilter(const_cast<ListDelegate* >(this));
    editor->setAcceptRichText(false);

    return editor;
}

void ListDelegate::setEditorData(QWidget* editor,
    const QModelIndex& index) const
{
    QTextEdit* _editor = qobject_cast<QTextEdit* >(editor);

    QString text = index.data(m_role).toString();
    _editor->setText(text);
    _editor->selectAll();
}

void ListDelegate::setModelData(QWidget* editor,
    QAbstractItemModel* model,
    const QModelIndex& index) const
{
    QTextEdit* _editor = qobject_cast<QTextEdit* >(editor);

    model->setData(index, _editor->toPlainText(), m_role);
}

void ListDelegate::updateEditorGeometry(QWidget* editor,
    const QStyleOptionViewItem& option,
    const QModelIndex& index) const
{
    editor->setGeometry(option.rect);
}
